package com.sskj.hangqing.ui.fragment;

import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerHorizenLineItemDecoration;
import com.sskj.common.util.DisposUtil;
import com.sskj.hangqing.R;
import com.sskj.hangqing.R2;
import com.sskj.hangqing.component.DaggerUserDataComponent;
import com.sskj.hangqing.presenter.SpecialCoinFragmentPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.enums.CoinEnum;
import com.sskj.lib.bean.enums.ColorEnum;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.util.CoinImgUtil;
import com.sskj.lib.util.CoinUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@SuppressWarnings("unchecked")
@Route(path = RConfig.HANG_FRAGMENT_SPECIAL_COIN)
public class SpecialCoinFragment extends BaseFragment<SpecialCoinFragmentPresenter> {
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    @BindView(R2.id.viewDot0)
    View viewDot0;
    @BindView(R2.id.viewDot1)
    View viewDot1;
    @BindView(R2.id.viewDot2)
    View viewDot2;
    @BindView(R2.id.recycle_up)
    RecyclerView recyclerView;

    private SlimAdapter<CoinBean> slimAdapter;
    private Disposable coinDispo;


    @Override
    protected int getLayoutId() {
        return R.layout.hang_fragment_special_coin;
    }

    @Override
    public SpecialCoinFragmentPresenter getPresenter() {
        return new SpecialCoinFragmentPresenter();
    }

    @Override
    public void initView() {
        initRecycle();
    }

    @Override
    public void initData() {
        mPresenter.getData();
        LiveDataBus.get().with(RxBusCode.PUSH_COIN_BEAN_BIBI, CoinBean.class)
                .observe(this, this::refreshCoin);
    }


    private void initRecycle() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        recyclerView.addItemDecoration(new DividerHorizenLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintColor(color(R.color.transparent))
                .setPaintWidth((int) RudenessScreenHelper.dp2px(App.INSTANCE, 15)));
        slimAdapter = SlimAdapter.create().register(R.layout.hang_item_home, new SlimInjector<CoinBean>() {
            @Override
            public void onInject(CoinBean coinBean, IViewInjector iViewInjector, List payloads) {
                if (payloads != null && !payloads.isEmpty()) {
                } else {
                    Glide.with(SpecialCoinFragment.this)
                            .load(CoinImgUtil.img.get(coinBean.getCode()))
                            .apply(new RequestOptions().error(CoinEnum.getByCode(coinBean.getCode()).getRes()))
                            .into((ImageView) iViewInjector.findViewById(R.id.ivCoinTypeFirst));
                }
                BigDecimal bigDecimal = new BigDecimal(coinBean.getPrice()).setScale(CoinUtil.getPriceKeepNum(coinBean.getCode()));

                iViewInjector
                        .background(R.id.hang_main, coinBean.isUp()?R.mipmap.hang_icon_coin_special_green:R.mipmap.hang_icon_coin_special_red)
                        .text(R.id.tvCoinTypeFirst, CoinUtil.showName(coinBean.getCode()))
                        .text(R.id.tvCoinUSDTFirst, bigDecimal.toPlainString())
                        .text(R.id.tvCoinRmbFirst, "≈" + coinBean.getCnyPrice() + " CNY")
                        .text(R.id.tvCoinRateFirst, coinBean.getChangeRate())
                        .textColor(R.id.tvCoinUSDTFirst, ColorEnum.getColor(coinBean.isUp()))
                        .textColor(R.id.tvCoinRateFirst, ColorEnum.getColor(coinBean.isUp()));
//                if (CoinImgUtil.img.containsKey(coinBean.getCode())) {
//                    ImageUtil.setImage(CoinImgUtil.img.get(coinBean.getCode()), (ImageView) iViewInjector.findViewById(R.id.ivCoinTypeFirst));
//                }
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView, i, view) ->
                        ARouter.getInstance().build(RConfig.HANG_MARKET)
                                .withString(Constans.CODE, slimAdapter.getDataItem(i).getCode())
                                .withString(Constans.QU, "/USDT")
                                .withBoolean(Constans.IS_BIBI, false)

                                .navigation());
    }

    public void updateCoin(List<CoinBean> coinBeanList) {
        if (coinBeanList != null) {
            coinDispo = Flowable.fromIterable(coinBeanList)
                    .take(3)
                    .toList()
                    .subscribe((coinBeans, throwable) -> slimAdapter.updateData(coinBeans));
        }
    }

    public void refreshCoin(CoinBean coinBean) {
        if (slimAdapter == null || slimAdapter.getData() == null || slimAdapter.getData().size() == 0) {
            return;
        }
        coinDispo = Flowable.fromIterable(slimAdapter.getData())
                .filter(item -> item.getCode().equals(coinBean.getCode()))
                .subscribe(item -> {
                    int position = slimAdapter.getData().indexOf(item);
                    slimAdapter.getData().set(position, coinBean);
                    slimAdapter.notifyItemChanged(position, 1);
                }, e -> {
                });
    }

    @Override
    public void onDestroy() {
        DisposUtil.close(coinDispo);
        super.onDestroy();
    }
}